<?php
/**
 * yui string validation
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: module.event.xhr.class.php 3 2010-05-27 10:39:05Z SRuttloff $
 * @link      ERP
 */

/**
 * string validation class
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      ERP
 */
class validateString
{
    /**
     * check a string for validity
     *
     * @param string $string string to validate
     * @param int    $i_min  min. string length, default = 2
     * @param int    $i_max  max. string length, default = 65
     *
     * @return string|bool returns cleaned string or false if something is wrong
     */
    public static function withMinMaxLength($string = null, $i_min = 2, $i_max = 65)
    {
        if ($i_min > $i_max) $i_min = $i_max = 0;
        if (!is_numeric($i_min)) $i_min = 2;
        if (!is_numeric($i_max)) $i_max = 65;
        $string = self::cleanString($string);
        // check string length
        $len = strlen($string);
        if ($len < $i_min || $len > $i_max)
            return false;
        return $string;
    }
    /**
     * check a string for nums and range
     *
     * @param string $string string to validate
     * @param int    $i_min  min. value, default = 0
     * @param int    $i_max  max. value, default = 1000
     *
     * @return string|bool returns cleaned string or false if something is wrong
     */
    public static function numWithin($string = null, $i_min = 0, $i_max = 1000)
    {
        if ($i_min >= $i_max) $i_min = $i_max = 0;
        if (!is_numeric($i_min)) $i_min = 0;
        if (!is_numeric($i_max)) $i_max = 1000;
        $string = self::cleanString($string);
        if (!is_numeric($string)) return false;
        // check range
        if ($string < $i_min || $string > $i_max) return false;
        return $string;
    }
    /**
     * removes htmltags and spaces around a string
     *
     * @param string $string string to clean
     *
     * @return string
     */
    public static function cleanString($string = null)
    {
        return strip_tags(trim($string));
    }
    /**
     * check if a string is a valid email address
     *
     * @param string $email string to validate
     *
     * @return string|bool email or false if wrong
     */
    public static function checkEmail($email = null)
    {
        $email = self::cleanString($email);
        $valid = "^([a-z0-9_]|\\-|\\.)+@(([a-z0-9]|\\-){3,}\\.)+[a-z\.]{2,8}$";
        if (eregi($valid, $email)) return $email;
        return false;
    }
    /**
     * Checks a given string for numbers only
     *
     * @param string $string check if nums only inside
     *
     * @return bool
     */
    public static function numsOnly($string = null)
    {
        $valid = "=^[0-9]+$=s";
        return preg_match($valid, $string);
    }
}